﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace BugTracker.Forms
{
    public partial class connectForm : Form
    {
        public connectForm()
        {
            InitializeComponent();
        }

        private void connectForm_Load(object sender, EventArgs e)
        {

        }

        public String UserName;
        public String Password;
        public String DSN;
        public bool RememberMe;

        private void connectForm_Shown(object sender, EventArgs e)
        {
            UserNameBox.Text = UserName;
            PasswordBox.Text = Password;
            dbCombo.Text = DSN;
            Height = 156;
            groupBox2.Visible = false;
            groupBox2.Enabled = false;

            // Получаю список всех DSN
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\ODBC\\ODBC.INI\\ODBC Data Sources\\");
            if (key != null)
                foreach (String valueName in key.GetValueNames())
                    dbCombo.Items.Add(valueName);

            if (UserName.Length > 0)
                PasswordBox.Focus();
        }

        private void advancedBtn_Click(object sender, EventArgs e)
        {
            if (Height == 212)
            {
                Height = 156;
                groupBox2.Visible = false;
                groupBox2.Enabled = false;
            } else
            {
                Height = 212;
                groupBox2.Visible = true;
                groupBox2.Enabled = true;
            };
        }

        private void connectForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserName = UserNameBox.Text;
            Password = PasswordBox.Text;
            DSN = dbCombo.Text;
            RememberMe = rememberChk.Checked;
        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            DBWork.DSN = dbCombo.Text;
            DBWork.Connect(UserNameBox.Text, PasswordBox.Text);
            if (DBWork.Active)
                DialogResult = DialogResult.OK;
            else
            {
                MessageBox.Show(DBWork.ErrorStr, "Connection Error", MessageBoxButtons.OK);
                DialogResult = DialogResult.None;
            };
        }
    }
}
